<script setup lang="ts">
import { downloadFileAPI, getEnterpriseDetailAPI } from "@/apis/enterprise";
import type { EnterpriseDetail, Rent } from "@/types/enterprise";
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";

const route = useRoute();

const detail = ref<EnterpriseDetail>({} as EnterpriseDetail);

const getDetail = async () => {
  if (route.query.id) {
    // 根据 id 获取企业详情
    const res = await getEnterpriseDetailAPI(route.query.id as string);
    // 保存企业详情2222
    detail.value = res.data;
  }
};

onMounted(() => {
  getDetail();
});

// 获取预览链接
const getPreviewUrl = (url: string) => {
  // 判断路径中文件后缀是是否为 pdf 还是 doc
  const suffix = url.split(".").pop();
  // doc 需要拼接链接，利用微软提供的免费 office 服务预览
  if (suffix === "doc") {
    const previewURL = "https://view.officeapps.live.com/op/view.aspx?src=";
    return previewURL + encodeURIComponent(url);
  }
  // pdf 无需处理，直接返回即可，浏览器支持预览 pdf
  return url;
};

// 下载文件
const downloadFile = async (row: Rent) => {
  // 根据附件 id 下载文件，返回二进制的 blob 数据
  const res = await downloadFileAPI(row.contractId);
  // 下载文件用 a 标签
  const aElement = document.createElement("a");
  // 把 blob 数据转换成 url 链接（类似鸿蒙的沙箱路径）
  aElement.href = window.URL.createObjectURL(res);
  // 指定下载后的文件名
  aElement.download = row.contractName || "";
  // 点击下载
  aElement.click();
  // 释放 Blob URL，避免内存泄漏
  window.URL.revokeObjectURL(aElement.href);
  // 移除用于下载的 a 标签
  aElement.remove();
};
</script>

<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div>{{ route.query }}</div>
        <div class="title">租赁记录</div>
        <div class="table-container">
          <!-- 企业租赁记录 - 表格 -->
          <el-table :data="detail.rent" style="width: 100%" :border="true">
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column prop="name" label="租赁楼宇323" width="180" />
            <el-table-column label="租赁起止时间" width="280">
              <template #default="{ row }">
                {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column label="租赁合同(点击预览)">
              <template #default="{ row }">
                <el-button type="text">
                  <!-- 新窗口打开预览 target="_blank" -->
                  <a :href="getPreviewUrl(row.contractUrl)" target="_blank">
                    {{ row.contractName }}
                  </a>
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" />
            <el-table-column prop="address" label="操作">
              <template #default="{ row }">
                <el-button type="text" @click="downloadFile(row)"
                  >合同下载</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>
  </div>
</template>

<style scoped lang="scss">
.detail-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      margin-bottom: 20px;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
    }

    .table-container {
      padding: 20px;
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }
}
</style>
